﻿using System;
using GeoAPI.Geometries;
using NetTopologySuite.Algorithm.Distance;

namespace NetTopologySuite.Algorithm.Match
{
	public class HausdorffSimilarityMeasure : ISimilarityMeasure
	{
		public double Measure(IGeometry g1, IGeometry g2)
		{
			double num = DiscreteHausdorffDistance.Distance(g1, g2, HausdorffSimilarityMeasure.DensifyFraction);
			Envelope envelope = new Envelope(g1.EnvelopeInternal);
			envelope.ExpandToInclude(g2.EnvelopeInternal);
			double num2 = HausdorffSimilarityMeasure.DiagonalSize(envelope);
			return 1.0 - num / num2;
		}

		public static double DiagonalSize(Envelope env)
		{
			if (env.IsNull)
			{
				return 0.0;
			}
			double width = env.Width;
			double height = env.Height;
			return System.Math.Sqrt(width * width + height * height);
		}

		private static readonly double DensifyFraction = 0.25;
	}
}
